#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node  {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );

int main()
{
    Polynomial a, b, s;
    a = Read();
    b = Read();
    s = Add(a, b);
    Print(s);
    return 0;
}

/* Your function will be put here */
Polynomial creat()
{
    Polynomial ans=(Polynomial)malloc(sizeof(struct Node));
    ans->Next=NULL;
    ans->Coefficient=0;
    ans->Exponent=0;
    return ans;
}
Polynomial Add( Polynomial a, Polynomial b )
{
    Polynomial ans=creat(),tail=ans;
    for(Polynomial ai=a->Next,bi=b->Next;ai||bi;)
    {
        Polynomial tmp=creat();
        if(ai&&bi&&ai->Exponent==bi->Exponent)
        {
            tmp->Exponent=ai->Exponent;
            tmp->Coefficient=ai->Coefficient+bi->Coefficient;
            ai=ai->Next;
            bi=bi->Next;
        }
        else if((ai&&(!bi))||(ai&&bi&&ai->Exponent>bi->Exponent))
        {
            tmp->Exponent=ai->Exponent;
            tmp->Coefficient=ai->Coefficient;
            ai=ai->Next;
        }
        else if((bi&&(!ai))||(ai&&bi&&ai->Exponent<bi->Exponent))
        {
            tmp->Exponent=bi->Exponent;
            tmp->Coefficient=bi->Coefficient;
            bi=bi->Next;
        }
        if(tmp->Coefficient==0)
            free(tmp);
        else
        {
            tail->Next=tmp;
            tail=tmp;
        }
    }
    return ans;
}